package com.xmg.dataStructure.tree.graph;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * 图
 */
public class Graph {

    private List<String> vertexList;    //存储顶点集合
    private int[][] edges;              //存储图对应的邻接矩阵
    private int numOfEdges;             //表示边的数目
    private boolean[] isVisited;//记录某个节点是否被访问
    public static void main(String[] args) {
        int n = 5;
        String vertexs[] = {"A","B","C","D","E"};
        Graph graph = new Graph(n);
        for (String vertex : vertexs) {
            graph.insertVertex(vertex);
        }
        graph.insertEdge(0,1,1);
        graph.insertEdge(0,2,1);
        graph.insertEdge(1,2,1);
        graph.insertEdge(1,3,1);
        graph.insertEdge(1,4,1);
        graph.printGraph();
//        graph.dfs();
        graph.bfs();
    }

    public Graph(int n){
        vertexList = new ArrayList<>(n);
        edges = new int[n][n];
        numOfEdges = 0;
        isVisited = new boolean[n];
    }

    //得到第一个邻接节点的下标
    public int getFirstNeighbor(int index){
        for (int i = 0; i < vertexList.size(); i++) {
            if(edges[index][i]>0){
                return i;
            }
        }
        return -1;
    }

    //根据前一个邻接节点的下标来获取下一个邻接节点
    public int getNextNeighbor(int v1,int v2){
        for (int i = v2 + 1; i < vertexList.size(); i++) {
            if(edges[v1][i]>0){
                return i;
            }
        }
        return -1;
    }

    //深度优先算法dfs
    public void dfs(boolean[] isVisited,int i){
        System.out.print(getValueByIndex(i)+"->");
        isVisited[i] = true;
        int w = getFirstNeighbor(i);
        while (w!=-1){
            if(!isVisited[i]){
                dfs(isVisited,w);
            }
            //如果w已经被访问过
            w = getNextNeighbor(i,w);
        }
    }

    //对dfs进行重载，遍历所有的节点，并进行dfs
    public void dfs(){
        //遍历所有的节点，进行dfs
        for (int i = 0; i < getNumofVertex(); i++) {
            if(!isVisited[i]){
                dfs(isVisited,i);
            }
        }
    }

    //对一个节点进行广度遍历
    private void bfs(boolean[] isVisited,int i){
        int u;//队列的头结点对应的下标
        int w;//邻接节点
        //队列，记录节点访问顺序
        LinkedList<Integer> queue = new LinkedList<>();
        //输出节点信息
        System.out.print(getValueByIndex(i)+"=>");
        isVisited[i] = true;
        queue.addLast(i);
        while (!queue.isEmpty()){
            //取出队列的头结点
            u = queue.removeFirst();
            w = getFirstNeighbor(u);
            while (w!=-1){
                if(!isVisited[w]){
                    System.out.print(getValueByIndex(w)+"=>");
                    isVisited[w] = true;
                    queue.addLast(w);
                }
                w = getNextNeighbor(u,w);
            }
        }
    }

    public void bfs(){
        for (int i = 0; i < getNumofVertex(); i++) {
            if(!isVisited[i]){
                bfs(isVisited,i);
            }
        }
    }


    public void printGraph(){
        for (int[] line:edges){
            System.out.println(Arrays.toString(line));
        }
    }

    //返回顶点个数
    public int getNumofVertex(){
        return vertexList.size();
    }
    //获取边的数目
    public int getNumOfEdges(){
        return numOfEdges;
    }
    //返回节点i对应的数据
    public String getValueByIndex(int i){
        return vertexList.get(i);
    }
    //返回v1和v2的权值
    public int getWeight(int v1,int v2){
        return edges[v1][v2];
    }
    //加入顶点
    public void insertVertex(String vertex){
        vertexList.add(vertex);
    }

    //添加边
    public void insertEdge(int v1,int v2,int weight){
        edges[v1][v2] = weight;
        edges[v2][v1] = weight;
        numOfEdges++;
    }
}
